package com.application.lucky.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.adapter.NewFriendsAdapter;
import com.application.lucky.bean.jsonBean.FriendApplyBean;
import com.application.lucky.bean.jsonBean.FriendSearchBean;
import com.application.lucky.presenter.NewFriendsPresenter;
import com.application.lucky.utils.ConnUtil;
import com.application.lucky.view.NewFriendsView;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.VerificationFriendDialog;
import com.application.mylibrary.base.mvp.BaseMvpActivity;
import com.application.mylibrary.utils.ToastUtils;
import com.application.mylibrary.widget.RefreshView;
import com.qunar.im.ui.view.recyclerview.BaseQuickAdapter;

import java.util.List;

public class NewFriendsActivity extends BaseMvpActivity<NewFriendsView, NewFriendsPresenter> implements NewFriendsView {

    private TitlebarView title;
    private RefreshView rfvView;
    private RecyclerView rclView;
    private View layout_empty;
    private NewFriendsAdapter adapter;
    private VerificationFriendDialog dialog;

    @Override
    public NewFriendsView createView() {
        return this;
    }

    @Override
    public NewFriendsPresenter createPresenter() {
        return new NewFriendsPresenter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initlistener();
        if (getPresenter() != null) {
            getPresenter().loadData();
        }
    }

    private void initlistener() {
        title.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        rfvView.setRefreshListener(new RefreshView.RefreshListener() {
            @Override
            public void onRefresh() {
                if (getPresenter() != null) {
                    getPresenter().loadData();
                }
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(final BaseQuickAdapter adapter, final View view, int position) {
                final FriendApplyBean.FriendListBean o = (FriendApplyBean.FriendListBean) adapter.getData().get(position);

                if (dialog == null) {
                    dialog = new VerificationFriendDialog(NewFriendsActivity.this, false);
                    dialog.setListener(new VerificationFriendDialog.AddListener() {
                        @Override
                        public void success(String id) {
                            if (isFinishing()) {
                                return;
                            }
                            dialog.dismiss();
                            NewFriendsActivity.this.adapter.upDataItemById(id);

                            insertBean(o);
                        }

                        @Override
                        public void fail(String uid, int code) {
                            if (isFinishing())
                                dialog.dismiss();
                            showToast("添加失败");
                        }
                    });
                }
                dialog.setUserId(o.getUserId() + "");
                dialog.showDialog();


            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FriendApplyBean.FriendListBean o = (FriendApplyBean.FriendListBean) adapter.getData().get(position);
                UserCenterActivity.jump(NewFriendsActivity.this, o.getUserId() + "", o.getUser().getNickName());
            }
        });
//        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                if (getPresenter() != null) {
//                    getPresenter().loadDataMore();
//                }
//            }
//        }, rclView);
    }

    @Override
    public void beforeInit() {

    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_new_friends;
    }

    private void initView() {
        title = findViewById(R.id.title);
        rfvView = findViewById(R.id.rfv_view);
        rclView = findViewById(R.id.rcl_view);
        layout_empty = findViewById(R.id.layout_empty);
        rclView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new NewFriendsAdapter(R.layout.item_new_friends_layout);
        rclView.setAdapter(adapter);
        layout_empty.setVisibility(View.VISIBLE);
        rfvView.setVisibility(View.INVISIBLE);
    }


    @Override
    public void setNewData(List<FriendApplyBean.FriendListBean> datas) {
        rfvView.setFinish();
        adapter.setNewData(datas);
        if (ConnUtil.isNotEmpty(datas)) {
            layout_empty.setVisibility(View.GONE);
            rfvView.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void addData(List<FriendApplyBean.FriendListBean> datas) {
        adapter.addData(datas);

        if (datas.size() > 0) {
            adapter.loadMoreComplete();
        } else {
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void error(int code) {

    }

    @Override
    public void upDataItem(int position, FriendApplyBean.FriendListBean item) {
        adapter.setData(position, item);
    }


    @Override
    public void showToast(String message) {
        ToastUtils.show(message);
    }

    private void insertBean(final FriendApplyBean.FriendListBean o) {
        if (o == null) {
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                FriendSearchBean bean = new FriendSearchBean(o);
                Lucky.getApplication().getDaoSession().getFriendSearchBeanDao().insertOrReplace(bean);
            }
        }).start();
    }
}
